package com.yixing.tech.system.controller;

import com.yixing.tech.common.base.*;
import com.yixing.tech.common.service.CommonService;
import com.yixing.tech.system.service.IDictService;
import com.yixing.tech.common.vo.s3.DictVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.yixing.tech.common.security.context.UserContext.getTenantId;

/**
 * @author yixing tech
 * at 2021/12/20
 * describe：
 */
@RestController
@CrossOrigin
@RequestMapping(value = "/dict")
public class DictController extends BaseControl {

    @Resource
    IDictService dictService;
    @Autowired
    private CommonService commonService;

    @PostMapping("/add")
    public Rs<Object> add(@RequestBody DictVO vo) throws BusinessException {
         commonService.fillCreateUser(vo);
        dictService.add(vo);
        return Rs.ok();
    }

    @PostMapping("/add/child")
    public Rs<Object> addChild(@RequestBody DictVO vo) throws BusinessException {
         commonService.fillCreateUser(vo);
        dictService.addChild(vo);
        return Rs.ok();
    }

    @PostMapping("/listPage")
    public Rs<Object> listPage(@RequestBody PageRow<DictVO> vo) throws BusinessException {
        vo.getCondition(DictVO.class).setTenantId(getTenantId());
        Page<DictVO> page = dictService.list(vo);
        return Rs.ok(page);
    }

    @PostMapping("/list")
    public Rs<Object> list(@RequestBody DictVO vo) throws BusinessException {
        vo.setTenantId(getTenantId());
        List<DictVO> list = dictService.list(vo);
        return Rs.ok(list);
    }

    @GetMapping("/{id}")
    public Rs<Object> get(@PathVariable String id) throws BusinessException {
        return Rs.ok(dictService.queryById(id));
    }

    @DeleteMapping("")
    public Rs<Object> delete(@RequestBody DictVO vo) throws BusinessException {
        dictService.delete(vo);
        return Rs.ok();
    }

    @PostMapping("/update/{id}")
    public Rs<Object> update(@RequestBody DictVO vo, @PathVariable String id) throws BusinessException {
        commonService.fillUpdateUser(vo);
        vo.setId(id);
        vo.setTenantId(getTenantId());
        dictService.update(vo);
        return Rs.ok();
    }


}
